#include "play-game-scene.h"
#include "ui_play-game-scene.h"

PlayGameScene::PlayGameScene(int level, QVector<QVector<bool>> map_data) :
    ui(new Ui::PlayGameScene)
{
    this->level = level;

    this->map_data = map_data;

    initUi();

    initData();

    initUiData();

    initConnect();
}

PlayGameScene::~PlayGameScene()
{
    delete ui;
}

void PlayGameScene::paintEvent(QPaintEvent *event)
{
    // 创建画家对象
    QPainter painter(this);

    // 加载主界面背景图片
    QPixmap pixmap_background(START_GAME_SCENE);
    painter.drawPixmap(0, 0, pixmap_background);

    // 加载主界面游戏标题
    QPixmap pixmap_title(TITLE);
    pixmap_title = pixmap_title.scaled( pixmap_title.width() * 0.6, pixmap_title.height() * 0.6);
    painter.drawPixmap(30, 20, pixmap_title);

    QFont font;
    font.setFamily("楷书");
    font.setPointSize(26);
    painter.setFont(font);

    // 显示左下角文字
    QString string = QString("Level:%1").arg(level);
    painter.drawText(QRect(30, this->height() - 55, 200, 50), string);
}


void PlayGameScene::initUi()
{
    ui->setupUi(this);

    // 创建返回按钮
    this->back_button = new ClickButton(BACK_BUTTON_NORMAL, BACK_BUTTON_SELECTED, BACK_BUTTON_SOUND);
    this->back_button->setParent(this);
    this->back_button->move(this->width() - back_button->width() - 20, this->height() - back_button->height() - 20);

    // 创建成功时显示的标签
    QPixmap pixmap_success(LEVEL_SUCCESS);
    this->success_label = new QLabel(this);
    this->success_label->setPixmap(pixmap_success);
    this->success_label->setFixedSize(pixmap_success.size());
    this->success_label->move(38, -100);

    // 给 success_label 创建动画
    this->game_success_animation = new QPropertyAnimation(this->success_label, "geometry");
    this->game_success_animation->setDuration(1000);
    this->game_success_animation->setStartValue(QRect(this->success_label->x(), this->success_label->y(), this->success_label->width(), this->success_label->height()));
    this->game_success_animation->setEndValue(QRect(38, 100, this->success_label->width(), this->success_label->height()));
    this->game_success_animation->setEasingCurve(QEasingCurve::OutBounce);

    // 创建 16 个硬币按钮
    for (int i = 0; i < 4; i++)
    {
        for (int j = 0; j < 4; j++)
        {
            // 创建硬币背景
            QLabel *coin_background = createCoinBackground();
            coin_background->setParent(this);
            coin_background->move(j * 55 + 50, i * 55 + 210);

            // 创建硬币按钮
            CoinButton *coin_button = new CoinButton(i, j, map_data[i][j]);
            coin_button->setParent(coin_background);
            coin_button->move(2, 3);

            // 保存银币的状体
            coin_button_list[i][j] = coin_button;
            coin_state_list[i][j] = coin_button->getCoinState();

            // 设置按钮点击事件
            connect(coin_button, &QPushButton::clicked, this, [=]() {
                this->coin_flip_sound->play();
                coin_button->flipCoin(50);
                coin_state_list[coin_button->x][coin_button->y] = coin_button->getCoinState();

                int next[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
                for (int i = 0; i < 4; i++) {
                    int x_next = coin_button->x + next[i][0];
                    int y_next = coin_button->y + next[i][1];

                    if (0 <= x_next && x_next < 4 && 0 <= y_next && y_next < 4) {
                        coin_button_list[x_next][y_next]->is_animation = true;
                        coin_state_list[x_next][y_next] = !coin_state_list[x_next][y_next];

                        QTimer::singleShot(400, this, [=]() {
                            coin_button_list[x_next][y_next]->flipCoin(50);
                        });
                    }
                }

                judgeGameSuccess();
            });
        }
    }
}


void PlayGameScene::initData()
{
    this->coin_flip_sound = new QSound(COIN_FILP_SOUND, this);

    this->game_success_sound = new QSound(GAME_SUCCESS_SOUND, this);
}

void PlayGameScene::initUiData()
{
    // 设置窗口固定大小
    this->setFixedSize(315, 560);

    // 设置窗口标题
    this->setWindowTitle("Coin Flip");

    // 设置窗口图标
    this->setWindowIcon(QPixmap(GOLD_COIN));
}

void PlayGameScene::initConnect()
{

}

void PlayGameScene::judgeGameSuccess()
{
    // 16 个硬币都为金币时游戏胜利
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
            if (!coin_state_list[i][j]) {
                return;
            }
        }
    }

    // 停止金币的功能
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
            coin_button_list[i][j]->finish();
        }
    }

    // 延时播放音效和动画
    QTimer::singleShot(400, this, [=]() {
        // 播放游戏完成时的音效
        this->game_success_sound->play();

        // 播放游戏完成时的动画
        this->game_success_animation->start();
    });
}

QLabel *PlayGameScene::createCoinBackground()
{
    QLabel *coin_background = new QLabel();

    QString image_pathname = COIN_BACKGROUND;

    QPixmap pixmap;
    bool result = pixmap.load(image_pathname);
    if(!result) {
        qDebug() << "加载图片 " << image_pathname << " 失败!";
        return nullptr;
    }

    // 图片设置缩放比
    pixmap = pixmap.scaled(pixmap.width() * 1, pixmap.height() * 1);

    // 设置图片固定大小
    coin_background->setFixedSize(pixmap.size());

    // 设置不规则图片样式
    coin_background->setStyleSheet("QPushButton{border:0px;}");

    // 设置图标
    coin_background->setPixmap(pixmap);

    return coin_background;
}
